﻿using System.Data;
using Marbles.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.CMS
{
    public class DataSourceContentManager : IContentManager
    {


        public string SelectPageCommandName
        {
            get { return this._SelectPageCommand; }
            set { this._SelectPageCommand = value; }
        }

        public string SelectContentCommandName
        {
            get { return this._SelectContentCommand; }
            set { this._SelectContentCommand = value; }
        }

        public string UpdateContentCommandName
        {
            get { return this._UpdateContentCommand; }
            set { this._UpdateContentCommand = value; }
        }

        public string InsertContentCommandName
        {
            get { return this._InsertContentCommand; }
            set { this._InsertContentCommand = value; }
        }

        public string DeleteContentCommandName
        {
            get { return this._DeleteContentCommand; }
            set { this._DeleteContentCommand = value; }
        }

        public string UpdatePageCommandName
        {
            get { return this._UpdatePageCommand; }
            set { this._UpdatePageCommand = value; }
        }

        public string InsertPageCommandName
        {
            get { return this._InsertPageCommand; }
            set { this._InsertPageCommand = value; }
        }

        public string DeletePageCommandName
        {
            get { return this._DeletePageCommand; }
            set { this._DeletePageCommand = value; }
        }

        private DataSource dbc = null;
        private string _DeleteContentCommand = string.Empty;
        private string _DeletePageCommand = string.Empty;
        private string _UpdatePageCommand = string.Empty;
        private string _InsertPageCommand = string.Empty;
        private string _SelectPageCommand = string.Empty;
        private string _InsertContentCommand = string.Empty;
        private string _SelectContentCommand = string.Empty;
        private string _UpdateContentCommand = string.Empty;

        public DataSourceContentManager(DataSource Datasource)
        {
            this.dbc = Datasource;
        }

        public DataSourceContentManager(DataSource Datasource, DataSourceContentManagerConfiguration Config)
        {
            this.dbc = Datasource;
            this._DeleteContentCommand = Config.DeleteContentCommand;
            this._DeletePageCommand = Config.DeletePageCommand;
            this._InsertContentCommand = Config.InsertContentCommand;
            this._InsertPageCommand = Config.InsertPageCommand;
            this._SelectContentCommand = Config.SelectContentCommand;
            this._SelectPageCommand = Config.SelectPageCommand;
            this._UpdateContentCommand = Config.UpdateContentCommand;
            this._UpdatePageCommand = Config.UpdatePageCommand;
        }

        public ContentPage SelectPage(string page)
        {
            ContentPage value = null;
            DataSourceCommand cmd = new DataSourceCommand(this._SelectPageCommand);
            cmd.Parameters.Add("@page", page);
            DataSet ds = dbc.FetchDataSet(cmd);

            if (ds.Tables.Count > 0)
            {
                value = new ContentPage();
                value.Page = page;
                value.Title = ds.Tables[0].Rows[0]["title"].ToString();

                foreach (DataRow dr in ds.Tables[1].Rows)
                {
                    ContentObject co = new ContentObject(dr["key"].ToString(), dr["content"].ToString());
                    value.ContentObjects.Add(co);
                }
            }

            return value;
        }

        public IContentObject SelectContent(string page, string key)
        {
            ContentObject value = null;
            DataSourceCommand cmd = new DataSourceCommand(this._SelectContentCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@key", key);
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0)
            {
                value = new ContentObject(key, dt.Rows[0]["content"].ToString());
            }

            return value;
        }

        public int UpdateContent(string page, IContentObject value)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._UpdateContentCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@key", value.Key);
            cmd.Parameters.Add("@value", ((ContentObject)value).Value);
            return dbc.ExecuteCommand(cmd);
        }

        public int AddContent(string page, IContentObject value)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._InsertContentCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@key", value.Key);
            cmd.Parameters.Add("@value", ((ContentObject)value).Value);
            return dbc.ExecuteCommand(cmd);
        }

        public int DeleteContent(string page, string key)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._DeleteContentCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@key", key);
            return dbc.ExecuteCommand(cmd);
        }

        public int DeletePage(string page)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._DeletePageCommand);
            cmd.Parameters.Add("@page", page);
            return dbc.ExecuteCommand(cmd);
        }

        public int AddPage(string page, string title)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._InsertPageCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@title", title);
            return dbc.ExecuteCommand(cmd);
        }

        public int UpdatePage(string page, string title)
        {
            DataSourceCommand cmd = new DataSourceCommand(this._UpdatePageCommand);
            cmd.Parameters.Add("@page", page);
            cmd.Parameters.Add("@title", title);
            return dbc.ExecuteCommand(cmd);
        }
    }
}
